package com.wedo.mall.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.e.library.activity.EActivity;
import com.wedo.mall.R;
import com.wedo.mall.fragment.MainFragment;
import com.wedo.mall.fragment.SellerFragment;
import com.wedo.mall.fragment.ShoppingFragment;
import com.wedo.mall.fragment.UserFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindViews;
import butterknife.OnClick;


public class MainActivity extends EActivity {

    @BindViews({R.id.tv_tab1, R.id.tv_tab2, R.id.tv_tab3, R.id.tv_tab4})
    List<TextView> tvs;
    @BindViews({R.id.iv_tab1, R.id.iv_tab2, R.id.iv_tab3, R.id.iv_tab4})
    List<ImageView> icons;

    private List<Fragment> fragments = new ArrayList<>(4);

    @Override
    protected int layout() {
        return R.layout.activity_main;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        disableSwipeBack();
        statusBar(R.color.e_purple);
        fragments.add(MainFragment.with());
        fragments.add(SellerFragment.with());
        fragments.add(ShoppingFragment.with());
        fragments.add(UserFragment.with());

        onTabChanged(0);

    }

    @OnClick({R.id.ll_tab1, R.id.ll_tab3, R.id.ll_tab2, R.id.ll_tab4})
    protected void onTabClick(View view) {
        switch (view.getId()) {
            case R.id.ll_tab1:
                onTabChanged(0);
                break;
            case R.id.ll_tab2:
                onTabChanged(1);
                break;
            case R.id.ll_tab3:
                onTabChanged(2);
                break;
            case R.id.ll_tab4:
                onTabChanged(3);
                break;
        }

    }

    private void onTabChanged(int position) {

        final FragmentManager fm = getSupportFragmentManager();
        final FragmentTransaction tr = fm.beginTransaction();

        int count = tvs.size();
        for (int i = 0; i < count; i++) {
            icons.get(i).setSelected(false);
            tvs.get(i).setSelected(false);
            Fragment fragment = fragments.get(i);
            if (!fragment.isHidden()) {
                tr.hide(fragment);
            }
        }

        icons.get(position).setSelected(true);
        tvs.get(position).setSelected(true);

        Fragment fragment = fragments.get(position);
        if (fragment.isAdded()) {
            tr.show(fragment).commitAllowingStateLoss();
        } else {
            tr.add(R.id.fl_fragment, fragment).show(fragment).commitAllowingStateLoss();
        }

    }

    private long mExitTime = System.currentTimeMillis();

    private void onDoublePressExitApp() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            toast("再按一次退出程序");
            mExitTime = System.currentTimeMillis();

        } else {
            defaultFinish();
        }
    }

    @Override
    public void onBackPressed() {
        onDoublePressExitApp();
    }

}
